package com.atguigu.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 满超
 * @date 2020/06/01
 **/
@Configuration
public class ElasticSearchConfig {

    /**
     * RequestOptions类保留应在同一应用程序中的许多请求之间共享的部分请求。 您可以创建一个单例实例，并在所有请求之间共享它：
     */
    public static final RequestOptions COMMON_OPTIONS;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        //builder.addHeader("Authorization", "Bearer " + TOKEN); 添加所有请求所需的所有标头。
        //自定义响应使用者。
        //builder.setHttpAsyncResponseConsumerFactory(
        //        new HttpAsyncResponseConsumerFactory
        //                .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }


    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //初始化客户端配置
        return new RestHighLevelClient(
                RestClient.builder(
                        //集群
                        //new HttpHost("127.0.0.1", 9201, "http"),
                        //new HttpHost("127.0.0.1", 9202, "http"),
                        new HttpHost("127.0.0.1", 9200, "http")
                ));
    }
}
